package com.erpoa.service.impl;

import com.erpoa.annotation.DataBase;
import com.erpoa.constants.DataSourceNames;
import com.erpoa.entity.PackMessage;
import com.erpoa.mapper.FactoryUtilMapper;
import com.erpoa.service.FactoryUtilService;
import com.erpoa.utils.PDAException;
import com.erpoa.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;

/**
 * @author chenjimin
 * @Title: FactoryUtilServiceImpl
 * @ProjectName oaerp
 * @Description: TODO
 * @date 2020/9/2116:36
 */
@Service
public class FactoryUtilServiceImpl implements FactoryUtilService {

    private static final Logger logger = LoggerFactory.getLogger(FactoryUtilServiceImpl.class);

    @Autowired
    private FactoryUtilMapper factoryUtilMapper;

    /**
     * 操作人员检验
     *
     * @param hrno
     * @return
     */
    @Override
    @DataBase(DataSourceNames.DSX5)
    @Transactional()
    public boolean checkUserValid(String hrno) {
        // 录入人员检验
        String flag = this.factoryUtilMapper.selectcheckUserValid(hrno);
        if (StringUtil.isEmpty(flag) || !flag.equals("Y")) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 获得该条码的总数量，用于判断是否是最后一张标签或者排程是否超量，因为交接的时候条码会被拆分
     *
     * @param packMessage
     * @return
     */
    @Override
    @DataBase(DataSourceNames.DSX5)
    @Transactional()
    public int getBarCodeTotalNum(PackMessage packMessage) {

        Integer num = this.factoryUtilMapper.getBarCodeTotalNum(packMessage.getBarcode());
        if (num <= 0) {
            throw new PDAException("条码未找到数据");
        }
        return num;

    }

    @Override
    @DataBase(DataSourceNames.DSX5)
    @Transactional()
    public boolean checkUserAndBarcode(PackMessage packMessage, boolean isRefillTwo) {

        boolean checkUser = this.checkUserValid(packMessage.getJobnum());
        if (checkUser) {
            boolean checkBarcode = this.checkBarcode(packMessage.getBarcode());
            System.out.println("在职员工√");
            logger.info("在职员工√");
            if (checkBarcode) {
                if (!isRefillTwo) {
                    boolean checkBelong = this.checkUserBelongApartment(packMessage.getJobnum(), packMessage.getBarcode());
                    System.out.println("条码检验成功");
                    if (checkBelong) {// 三个基本验证通过后
                        System.out.println("人员与部门匹配检验成功");
                        return true;
                    } else {
                        System.out.println("checkUserBelongApartment失败");
                    }
                } else {
                    return true;
                }
            } else {
                System.out.println("条码不存在×");
            }
        } else {
            System.out.println("非有效在职员工×");
        }
        return false;

    }

    /**
     * 条码存在性检验
     *
     * @param barcode
     * @return
     */
    @Override
    @DataBase(DataSourceNames.DSX5)
    @Transactional()
    public boolean checkBarcode(String barcode) {
        int checkBarcodeint = this.factoryUtilMapper.checkBarcode(barcode);
        if (checkBarcodeint == 0) {
            return false;
        }
        return true;
    }

    /**
     * 人员是否属于本部门
     *
     * @param jobnum
     * @param barcode
     * @return
     */
    @Override
    @DataBase(DataSourceNames.DSX5)
    @Transactional()
    public boolean checkUserBelongApartment(String jobnum, String barcode) {

        int vaildint = this.factoryUtilMapper.checkUserBelongApartment(barcode, jobnum);
        if (vaildint == 0) {
            System.out.println("操作工不属于本部门");
            return false;
        }
        return true;
    }

    /**
     * 排程尚欠数与状态检验(定量包装扫描通用)
     *
     * @param packMessage
     * @return
     */
    @Override
    @DataBase(DataSourceNames.DSX5)
    @Transactional()
    public String checkScheduleUnqtyAndStatus(PackMessage packMessage) {

        HashMap vaildsqltemp = this.factoryUtilMapper.vaildsqlByBarcode(packMessage.getBarcode());
        String message = "NORMAL";
        Integer mUnFinQty = 0;
        try {
            boolean flag = false;
            mUnFinQty = vaildsqltemp.get("FUNFINQTY") == null ? 0 : (Integer) vaildsqltemp.get("FUNFINQTY");
            if (("生产中").equals(vaildsqltemp.get("FORDERSTATUS"))) {
                int sumqty = 0; //排程任务数
                int fqty = 0;    //排程已完成数
                int theqty = 0; //本次完成数
                int fpacknum = 0;//装箱标准
                flag = false;
                HashMap sql1Temp = this.factoryUtilMapper.selectplanpackByBarCode(packMessage.getBarcode());
                if (StringUtil.isNotNull(sql1Temp)) {
                    sumqty = (int) sql1Temp.get(1);
                    fpacknum = (int) sql1Temp.get(2);
                    flag = true;
                }

                if (!flag) {
                    throw new PDAException("该条码不存在!");
                } else {
                    fqty = this.factoryUtilMapper.sumfqtyByBarCode(packMessage.getBarcode());

                    theqty = this.factoryUtilMapper.sumfqty3ByBarCode(packMessage.getBarcode());

                    System.out.println("排程任务数=" + sumqty + "\t排程已完成数=" + fqty + "\t本次完成数=" + theqty);
                    if ((fqty + theqty) > sumqty) {
                        throw new PDAException("条码总产出量会超出排程任务数");
                    }
                    //2018-11-14 liujiajie 加一个装箱标准管控
                    if (theqty > fpacknum) {
                        throw new PDAException("条码产出已超装箱标准!");
                    }
                }

            } else {
                message = "排程不在生产中!";
            }

            if (!flag) {
                return "条码所属排程不存在!" + "##" + mUnFinQty;
            } else {
                return message + "##" + mUnFinQty;
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            logger.info(e.getMessage());
            return e.getMessage() + "##" + mUnFinQty;
        }

    }

    /**
     * 是否是正常工单
     *
     * @param barcode
     * @return
     */

    @Override
    @DataBase(DataSourceNames.DSX5)
    @Transactional()
    public boolean isNormalOrder(String barcode) {
        try {

            String orderNo = this.factoryUtilMapper.isNormalOrderByBarCode(barcode);
            if (StringUtil.isEmpty(orderNo)) {
                throw new PDAException("没有找到条码数据");
            }

            // 为B就是返工工单
            boolean flag = "B".equals(orderNo.substring(2, 3));
            return !flag;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            logger.info(e.getMessage());
            return false;
        }
    }

    /**
     * 校验ERP中是否已经发满
     *
     * @param
     * @return
     */
    @Override
    @DataBase(DataSourceNames.DSX5)
    @Transactional()
    public int checkErpEnd(String orderno) {
        // ERP中是否已经发满
        int flag = this.factoryUtilMapper.countsfa01ByorderNo(orderno);
        return flag;
    }


}
